#!/bin/bash

# stop exist setvice if alive
dir1="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
$dir1/stop.sh
rm -rf $dir1/maven-metadata.xml*  $dir1/*.jar*

URL=http://10.200.1.206:8081/artifactory/PLATFORM-CORE-SNAPSHOT/com/lvmama/vst/msdemo-service/1.0.0-SNAPSHOT
wget -O $URL/maven-metadata.xml

# download jar file
BUILD_VER=`cat ./maven-metadata.xml|grep "<value>"|uniq|cut -d"<" -f2|cut -c7-30`
jarFile="msdemo-service-${BUILD_VER}.jar"
echo wget $URL/$jarFile
wget $URL/$jarFile

# run service account to command line
JAVA_OPTS="  -Xms1g -Xmx1g -XX:PermSize=128m -XX:MaxPermSize=256m "
for i in $*
        do echo $i
        arg3=`echo $i | grep "\-D"`
        if [ ! -z "$arg3" ]; then
                JAVA_OPTS="$JAVA_OPTS $i"
        fi
done
echo "JAVA_OPTS $JAVA_OPTS"
nohup java $JAVA_OPTS -jar $jarFile > "$dir1/msdemo-service.output" 2>&1 &

# wait until application service is started
COUNTER=0  

while [  $COUNTER -lt 10 ]; do  
	sleep 30
	msg=`grep "Started DemoApplication" "$dir1/msdemo-service.output"`
	if [[ "xxxx$msg" = "xxxx" ]]; 
	then
     		let COUNTER=COUNTER+1   
	else
		sec=`expr $COUNTER \* 30`
		echo "application started in $sec seconds"
		sleep 10
		let COUNTER=100
	fi
done  

